package com.cvte.boe.media.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liyang on 2014/11/6.
 */
public class MemInfoUtil {

    private MemInfoUtil() {
    }

    private static MemInfoUtil single = null;

    public synchronized static MemInfoUtil getInstance() {
        if (single == null) {
            single = new MemInfoUtil();
            initMemInfo();
        }
        return single;
    }

    private final String TAG = "DeviceInfoUtil";
    private static Map<String, String> meminfoMap = null;

    private static void initMemInfo() {
        if (meminfoMap == null)
            meminfoMap = new HashMap<String, String>();
        String str1 = "/proc/meminfo";
        String str2 = "";
        String[] infoArray;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            while ((str2 = localBufferedReader.readLine()) != null) {
                infoArray = str2.split(":");
                meminfoMap.put(infoArray[0].trim(), infoArray[1].trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取内存信息大小
     *
     * @param key MemTotal <br />
     *            MemFree <br />
     *            Buffers<br />
     *            Cached <br />
     *            SwapCached <br />
     *            Active<br />
     *            Inactive <br />
     *            Active(anon) <br />
     *            Inactive(anon)<br />
     *            Active(file)<br />
     *            Inactive(file)<br/>
     *            Unevictable<br/>
     *            Mlocked<br/>
     *            HighTotal<br/>
     *            HighFree<br/>
     *            LowTotal<br/>
     *            LowFree<br/>
     *            SwapTotal<br/>
     *            SwapFree<br/>
     *            Dirty<br/>
     *            Writeback<br/>
     *            Mapped<br/>
     *            Shmem<br/>
     *            Slab<br/>
     *            SReclaimable<br/>
     *            SUnreclaim<br/>
     *            KernelStack<br/>
     *            PageTables<br/>
     *            NFS_Unstable<br/>
     *            Bounce<br/>
     *            WritebackTmp<br/>
     *            CommitLimit<br/>
     *            Committed_AS<br/>
     *            VmallocTotal<br/>
     *            VmallocUsed<br/>
     *            VmallocChunk<br/>
     * @return
     */
    public String getMemInfo(String key) {
        return meminfoMap.get(key);
    }

    /**
     * 获取内存信息大小(kB)
     *
     * @param key MemTotal <br />
     *            MemFree <br />
     *            Buffers<br />
     *            Cached <br />
     *            SwapCached <br />
     *            Active<br />
     *            Inactive <br />
     *            Active(anon) <br />
     *            Inactive(anon)<br />
     *            Active(file)<br />
     *            Inactive(file)<br/>
     *            Unevictable<br/>
     *            Mlocked<br/>
     *            HighTotal<br/>
     *            HighFree<br/>
     *            LowTotal<br/>
     *            LowFree<br/>
     *            SwapTotal<br/>
     *            SwapFree<br/>
     *            Dirty<br/>
     *            Writeback<br/>
     *            Mapped<br/>
     *            Shmem<br/>
     *            Slab<br/>
     *            SReclaimable<br/>
     *            SUnreclaim<br/>
     *            KernelStack<br/>
     *            PageTables<br/>
     *            NFS_Unstable<br/>
     *            Bounce<br/>
     *            WritebackTmp<br/>
     *            CommitLimit<br/>
     *            Committed_AS<br/>
     *            VmallocTotal<br/>
     *            VmallocUsed<br/>
     *            VmallocChunk<br/>
     * @return
     */
    public int getIntMemInfo(String key) {
        String val = meminfoMap.get(key);
        return (val != null && val.length() > 0) ? Integer.parseInt(val.split("\\s+")[0]) : 0;
    }

}
